package com.exp.sys.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import tk.mybatis.mapper.entity.Example;

import com.exp.base.annotation.TransactionalRead;
import com.exp.base.annotation.TransactionalWrite;
import com.exp.sys.entity.Admin;
import com.exp.sys.entity.AdminRole;
import com.exp.sys.entity.AdminRole.AdminRoleId;
import com.exp.sys.entity.Role;
import com.exp.sys.mapper.AdminMapper;
import com.exp.sys.mapper.RoleMapper;
import com.exp.sys.vo.admin.AdminVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;

@Component
public class AdminService extends BaseService<Admin,AdminMapper> {
	
	@Override
	@Resource
	public void setMapper(AdminMapper mapper) {
		super.setMapper(mapper);
	}
	@TransactionalRead
	public AdminVo getVo(String id){
		return mapper.getVo(id);
	}
	public void saveVo(AdminVo adminVo){
		save(adminVo);
		List<String> idRoleList = adminVo.getRoleIdList();
		mapper.deleteAdminRole(Lists.newArrayList(adminVo.getId()));
		List<AdminRoleId> adminRoleList= new ArrayList<AdminRoleId>();
		for(String roleId : idRoleList ){
			AdminRoleId adminRole = new AdminRoleId();
			adminRole.setAdminId(adminVo.getId());
			adminRole.setRoleId(roleId);
			adminRoleList.add(adminRole);
		}
		mapper.insertAdminRole(adminRoleList);
	
	}
	public void updateVo(AdminVo adminVo){
		update(adminVo);
		List<String> idRoleList = adminVo.getRoleIdList();
		mapper.deleteAdminRole(Lists.newArrayList(adminVo.getId()));
		List<AdminRoleId> adminRoleList= new ArrayList<AdminRoleId>();
		for(String roleId : idRoleList ){
			AdminRoleId adminRole = new AdminRoleId();
			adminRole.setAdminId(adminVo.getId());
			adminRole.setRoleId(roleId);
			adminRoleList.add(adminRole);
		}
		mapper.insertAdminRole(adminRoleList);
	
	}
	
	public void deleteWithRelate(String[] ids){
		delete(ids);
		mapper.deleteAdminRole(Arrays.asList(ids));
	}
	
}
